{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "import seaborn as sns\n",
    "from sklearn.model_selection import train_test_split\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "from sklearn.metrics import mean_squared_error  # 评价\n",
    "import lightgbm as lgb"
   ]
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 读取数据"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b97670ee9e5e6cf0"
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "data = pd.read_csv(\"D:\\\\data\\\\seaborn-data\\\\iris.csv\")"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-05T07:03:54.161191300Z",
     "start_time": "2024-01-05T07:03:54.120407500Z"
    }
   },
   "id": "9bce3a0c377a7a01"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 数据基本处理"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "be9c314cf38e9368"
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [
    {
     "data": {
      "text/plain": "array(['setosa', 'versicolor', 'virginica'], dtype=object)"
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[\"species\"].unique()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-05T07:03:55.439515400Z",
     "start_time": "2024-01-05T07:03:55.368602Z"
    }
   },
   "id": "6e3684334abb7905"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# data[\"species\"]正则化 "
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "c64003e84fb016c7"
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "     sepal_length  sepal_width  petal_length  petal_width    species\n0             5.1          3.5           1.4          0.2     setosa\n1             4.9          3.0           1.4          0.2     setosa\n2             4.7          3.2           1.3          0.2     setosa\n3             4.6          3.1           1.5          0.2     setosa\n4             5.0          3.6           1.4          0.2     setosa\n..            ...          ...           ...          ...        ...\n145           6.7          3.0           5.2          2.3  virginica\n146           6.3          2.5           5.0          1.9  virginica\n147           6.5          3.0           5.2          2.0  virginica\n148           6.2          3.4           5.4          2.3  virginica\n149           5.9          3.0           5.1          1.8  virginica\n\n[150 rows x 5 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>sepal_length</th>\n      <th>sepal_width</th>\n      <th>petal_length</th>\n      <th>petal_width</th>\n      <th>species</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5.1</td>\n      <td>3.5</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4.9</td>\n      <td>3.0</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4.7</td>\n      <td>3.2</td>\n      <td>1.3</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4.6</td>\n      <td>3.1</td>\n      <td>1.5</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5.0</td>\n      <td>3.6</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>145</th>\n      <td>6.7</td>\n      <td>3.0</td>\n      <td>5.2</td>\n      <td>2.3</td>\n      <td>virginica</td>\n    </tr>\n    <tr>\n      <th>146</th>\n      <td>6.3</td>\n      <td>2.5</td>\n      <td>5.0</td>\n      <td>1.9</td>\n      <td>virginica</td>\n    </tr>\n    <tr>\n      <th>147</th>\n      <td>6.5</td>\n      <td>3.0</td>\n      <td>5.2</td>\n      <td>2.0</td>\n      <td>virginica</td>\n    </tr>\n    <tr>\n      <th>148</th>\n      <td>6.2</td>\n      <td>3.4</td>\n      <td>5.4</td>\n      <td>2.3</td>\n      <td>virginica</td>\n    </tr>\n    <tr>\n      <th>149</th>\n      <td>5.9</td>\n      <td>3.0</td>\n      <td>5.1</td>\n      <td>1.8</td>\n      <td>virginica</td>\n    </tr>\n  </tbody>\n</table>\n<p>150 rows × 5 columns</p>\n</div>"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-05T07:03:56.841614Z",
     "start_time": "2024-01-05T07:03:56.824742100Z"
    }
   },
   "id": "d0592cf9d487cfa"
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "# 方法一，逐个替换\n",
    "data[\"target\"] = np.where(data[\"species\"] == \"setosa\", 0, np.where(data[\"species\"] == \"versicolor\", 1, 2))\n",
    "\n",
    "# 方法二：one-host处理：pd.get_dummies(复杂了)\n",
    "# data = pd.get_dummies(data,columns=[\"species\"])\n",
    "# data[\"target\"] = np.where(data[\"species_setosa\"], 0, np.where(data[\"species_versicolor\"], 1, 2))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-05T07:03:58.366014Z",
     "start_time": "2024-01-05T07:03:58.327017Z"
    }
   },
   "id": "3af07976d9afbeaa"
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [
    {
     "data": {
      "text/plain": "     sepal_length  sepal_width  petal_length  petal_width    species  target\n0             5.1          3.5           1.4          0.2     setosa       0\n1             4.9          3.0           1.4          0.2     setosa       0\n2             4.7          3.2           1.3          0.2     setosa       0\n3             4.6          3.1           1.5          0.2     setosa       0\n4             5.0          3.6           1.4          0.2     setosa       0\n..            ...          ...           ...          ...        ...     ...\n145           6.7          3.0           5.2          2.3  virginica       2\n146           6.3          2.5           5.0          1.9  virginica       2\n147           6.5          3.0           5.2          2.0  virginica       2\n148           6.2          3.4           5.4          2.3  virginica       2\n149           5.9          3.0           5.1          1.8  virginica       2\n\n[150 rows x 6 columns]",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>sepal_length</th>\n      <th>sepal_width</th>\n      <th>petal_length</th>\n      <th>petal_width</th>\n      <th>species</th>\n      <th>target</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>5.1</td>\n      <td>3.5</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>4.9</td>\n      <td>3.0</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>4.7</td>\n      <td>3.2</td>\n      <td>1.3</td>\n      <td>0.2</td>\n      <td>setosa</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>4.6</td>\n      <td>3.1</td>\n      <td>1.5</td>\n      <td>0.2</td>\n      <td>setosa</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>5.0</td>\n      <td>3.6</td>\n      <td>1.4</td>\n      <td>0.2</td>\n      <td>setosa</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>...</th>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n      <td>...</td>\n    </tr>\n    <tr>\n      <th>145</th>\n      <td>6.7</td>\n      <td>3.0</td>\n      <td>5.2</td>\n      <td>2.3</td>\n      <td>virginica</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>146</th>\n      <td>6.3</td>\n      <td>2.5</td>\n      <td>5.0</td>\n      <td>1.9</td>\n      <td>virginica</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>147</th>\n      <td>6.5</td>\n      <td>3.0</td>\n      <td>5.2</td>\n      <td>2.0</td>\n      <td>virginica</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>148</th>\n      <td>6.2</td>\n      <td>3.4</td>\n      <td>5.4</td>\n      <td>2.3</td>\n      <td>virginica</td>\n      <td>2</td>\n    </tr>\n    <tr>\n      <th>149</th>\n      <td>5.9</td>\n      <td>3.0</td>\n      <td>5.1</td>\n      <td>1.8</td>\n      <td>virginica</td>\n      <td>2</td>\n    </tr>\n  </tbody>\n</table>\n<p>150 rows × 6 columns</p>\n</div>"
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-01-05T07:04:02.060365100Z",
     "start_time": "2024-01-05T07:04:02.040400100Z"
    }
   },
   "id": "a50bf08e42dcfd4a"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "data.info"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "2cb79cb777d4a6af"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 确定x值，可以用一个个列取值，也可以用drop删除多余项\n",
    "# x = data[[\"sepal_length\",\"sepal_width\",\"petal_length\",\"petal_width\"]]\n",
    "x = data.drop([\"species\", \"target\"], axis=1)\n",
    "y = data[\"target\"]"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "fe4c828487b8cd6d"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=0.2)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b32ad8278d134a9e"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "x_train"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a5cdd774b0212643"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "y_train"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "376be9f1f8c9ea95"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 模型训练"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a63aaa1cbe658751"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "gbm = lgb.LGBMRegressor(\n",
    "    objective=\"regression\",\n",
    "    learning_rate=0.5,\n",
    "    n_estimators=200\n",
    ")\n",
    "\n",
    "gbm.fit(x_train,\n",
    "        y_train,\n",
    "        eval_set=[(x_test, y_test)],\n",
    "        eval_metric=\"l1\",  # 用l1正则化,也可以用l2....\n",
    "        # early_stopping_rounds=5 # 新版本已经没有这个了。。。\n",
    "        )\n",
    "gbm.score(x_test, y_test)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "62263756acb797bd"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "gbm = lgb.LGBMRegressor(\n",
    "    objective=\"regression\",\n",
    "    learning_rate=0.5,\n",
    "    n_estimators=20\n",
    ")\n",
    "\n",
    "gbm.fit(x_train,\n",
    "        y_train,\n",
    "        eval_set=[(x_test, y_test)],\n",
    "        eval_metric=\"l1\",  # 用l1正则化,也可以用l2....\n",
    "        # early_stopping_rounds=5 # 新版本已经没有这个了。。。\n",
    "        )\n",
    "gbm.score(x_test, y_test)\n",
    "y_pre = gbm.predict(x_test,num_iteration=gbm.best_iteration_)\n",
    "# 评价\n",
    "mean_squared_error(y_test,y_pre)\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "8e82051631c13bc4"
  },
  {
   "cell_type": "markdown",
   "source": [
    "# 通过网格搜索进行训练（二次调优）"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "86d60d5f0258e90d"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "estimator = lgb.LGBMRegressor(num_leaves=31)\n",
    "param_grid = {\n",
    "    \"learning_rage\": [0.01, 0.1, 0.5,1],\n",
    "    \"n_estmators\": [20, 40, 60, 80]\n",
    "}\n",
    "gbm = GridSearchCV(estimator, param_grid, cv=5)\n",
    "gbm.fit(x_train, y_train)\n",
    "y_pre = gbm.predict(x_test,num_iteration=gbm.best_iteration_)\n",
    "# 评价\n",
    "mean_squared_error(y_test,y_pre)"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "d586513eb421f9ad"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 查看最好的参数\n",
    "gbm.best_params_"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "78522fb1bd92ce0d"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "gbm = lgb.LGBMRegressor(\n",
    "    objective=\"regression\",\n",
    "    learning_rate=0.01,\n",
    "    n_estimators=200,\n",
    "    force_col_wise=True\n",
    ")\n",
    "gbm.fit(x_train,\n",
    "        y_train,\n",
    "        eval_set=[(x_test, y_test)],\n",
    "        eval_metric=\"l1\",  # 用l1正则化,也可以用l2....\n",
    "        # early_stopping_rounds=5 # 新版本已经没有这个了。。。\n",
    "        )\n",
    "gbm.score(x_test, y_test)\n",
    "\n"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b6cdfbe84db97f7f"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "9676068048879559\n",
    "9644131145254191"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "54c0f61d8be29c2"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "546d46c1d46e39f1"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "1df9f0ddd02b1b9d"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "701fcf0e385c1579"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "4e53bb577e60e10c"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
